package com.nowcoder.community.c_service;

import com.nowcoder.community.a_entity.Comment;
import com.nowcoder.community.a_entity.DiscussPost;
import com.nowcoder.community.a_entity.Event;
import com.nowcoder.community.b_dao.CommentMapper;
import com.nowcoder.community.c1_kafka.EventProducer;
import com.nowcoder.community.z_util.CommunityConstant;
import com.nowcoder.community.z_util.CommunityUtil;
import com.nowcoder.community.z_util.HostHolder;
import com.nowcoder.community.z_util.MinGanF;
import com.sun.org.apache.xml.internal.serializer.utils.SerializerMessages_zh_CN;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CommentService implements CommunityConstant{
    @Autowired
    private DiscussPostService discussPostService;//调另一个service+事务！！！！

    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private HostHolder hostHolder;
    @Autowired
    private MinGanF minganF;
    @Autowired
    private EventProducer eventProducer;

    public List<Comment> findComentRowByetype_eid__off_limt(int entityType, int entityId,
                                                            int offset, int limit){
        return commentMapper.selectCommentsByEntity(entityType,  entityId,  offset,  limit);
    }

    /**
     * <p>——————————————————————————————————————————————-————
     * <p>   【当前类】CommentService
     * <p>
     * <p>   【谁调用我】每个帖子下的 1级评论、2级评论功能
     * <p>
     * <p>   【调用我干什么】根据e_Type加1级评论、2级评论
     * <p>——————————————————————————————————————————————-————
     * @param comment
     */
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)//第一个是事务，第二个隔离是默认的没啥用
    public void addComment(Comment comment){
        //先mapper+xml再serveice 又jb忘，你不乱谁乱 qc

        //先（防注入标签攻击+处理敏感词后）插入12评论表
        MinGanF minGanWordUitl = new MinGanF();
        comment.setUserId(hostHolder.getUser().getId());
        comment.setStatus(0);//帖子状态正常，没jb用
        comment.setCreateTime(new Date());
        comment.setContent(minganF.guolv(comment.getContent()));//其实前面的数据也应该在service里写，在control写就分开了
        //插sql数据库
        commentMapper.insertComment(comment);



        //+插ES库 e_t e_id//评论也加入
        // 触发评论事件
        /**
         * <p>——————————————————————————————————————————————-————
         * <p> CommentService：只搜帖子，不搜评论了吧。。
         * <p>——————————————————————————————————————————————-————
         */

        //【如果是1级评论，才更新帖子回复数哦！】
        //用评论表查出评论数给帖子表用count
        if(comment.getEntityType()==1){
            //【这里巨难】
            //好难：e_type=1是1级回复，e_id是帖子id（post的主键id（com标主键id没用））
            int curCount = commentMapper.selectCountByEntity(1,comment.getEntityId());

            //再改帖子表的评论数
            discussPostService.updateCommentCount(comment.getId(),curCount);//1级回复的eid是帖子id，2级回复的edi是1及回复的id
        }
    }


    public Comment findCommentById(int id){
        return commentMapper.selectCommentById(id);
    }

}
